// work/pages/riCheng/riCheng.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    chooseTop: wx.getStorageSync('chooseTop') == '' ? '0' : wx.getStorageSync('chooseTop'), // 0月 1日程
    num: 0,
    date: '', // 日期
    scheduleList: []
  },

  // 选择
  choose(e) {
    var index = e.currentTarget.dataset.flag;
    this.setData({
      chooseTop: index
    })
  },
  // 添加跳转
  toHerf() {
    wx.navigateTo({
      url: '/work/pages/add_riCheng/add_riCheng',
    })
  },

  // 查看详情
  toDetail(e) {
    var obj = JSON.stringify(e.currentTarget.dataset.obj);
    wx.navigateTo({
      url: '/work/pages/detail_riCheng/detail_riCheng?obj=' + obj,
    })
  },
  toShow(e) {
    var scheduleList = this.data.scheduleList,
      index = e.currentTarget.dataset.index,
      flag = !this.data.scheduleList[index].show;
    scheduleList[index].show = flag
    this.setData({
      scheduleList
    })

  },

  // 选择日期
  change(e) {
    console.log(e);
    var obj = e.detail,
      scheduleList = this.data.scheduleList,
      timeStr = obj.year + '-' + obj.month + '-' + obj.day;
    console.log(new Date(timeStr).getTime());
    console.log(scheduleList);



  },

  // 切换月份
  changeMonth(e) {
    console.log(e.detail);
    var obj = e.detail,
      timeStr = obj.year + '-' + obj.month + '-' + obj.day;
    this.setData({
      num: this.checkMonth(timeStr)
    })
    this.getData()
  },
  checkMonth(month) {
    var startDate = new Date();
    var endDate = new Date(month.replace("-", "/").replace("-", "/"));

    var number = 0;
    var yearToMonth = (endDate.getFullYear() - startDate.getFullYear()) * 12;
    number += yearToMonth;
    var monthToMonth = endDate.getMonth() - startDate.getMonth();
    number += monthToMonth;

    console.log(number);
    if (number < 1) {
      console.log(Math.abs(number));
      return Math.abs(number)
    } else {
      return 0 - number
    }
  },

  // 比较时间
  checkTime(startTime, endTime, nowTime) {
    return new Date(startTime).getTime() <= new Date(nowTime).getTime() <= new Date(endTime).getTime()
  },

  // 获取数据
  getData() {
    var url = app.globalData.url + 'schedule/listSchedule',
      that = this;
    app.wxRequest(url, {
      memberId: app.globalData.memberId,
      num: that.data.num
    }, res => {
      if (res.flag) {
        var res = res.data,
          map = {},
          list = [],
          index = {},
          j = 0,
          scheduleList = [];
        for (var i in res) {
          if (!map[res[i].startDay]) {
            var curr = [];
            curr.push(res[i]);
            list.push({
              date_time: res[i].startDay,
              week_name: res[i].weekName,
              start_day: res[i].startDay,
              show: false,
              details: curr,
            })
            map[res[i].startDay] = res[i].startDay;
            index[res[i].startDay] = j;
            j++;
          } else {
            list[index[res[i].startDay]].details.push(res[i]);
          }
        }
        console.log(list);
        scheduleList = list;
        for (var i in scheduleList) {
          var afterTime = new Date(scheduleList[i].start_day.replace(/-/g, "/"));
          scheduleList[i].start_day = afterTime.getFullYear() + '年' + (afterTime.getMonth() +
            1) + '月' + afterTime.getDate() + '日';
          for (var j in scheduleList[i].details) {
            var time = new Date(scheduleList[i].details[j].startDay);
            scheduleList[i].details[j].startDay = time.getFullYear() + '年' + (time
              .getMonth() + 1) + '月' + time.getDate() + '日';
            var time1 = new Date(scheduleList[i].details[j].endDay);
            scheduleList[i].details[j].endDay = time1.getFullYear() + '年' + (time1
              .getMonth() + 1) + '月' + time1.getDate() + '日';
          }
        }
        scheduleList = scheduleList.sort((a, b) => {
          return new Date(b.date_time).getTime() - new Date(a.date_time).getTime()
        })
        console.log(scheduleList)
        that.setData({
          scheduleList
        })
        wx.stopPullDownRefresh()
      }
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getData()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})